package com.jiantong.web.open.app.news;

import com.jiantong.news.model.News;
import com.jiantong.news.service.NewsReadService;
import com.jiantong.news.service.NewsWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**首页\新闻
 * Created by mz03 on 2017/3/15.
 * @author :zyp
 */
@Slf4j
@OpenBean
public class NewsApis {
    @RpcConsumer
    private NewsWriteService newsWriteService;

    @RpcConsumer
    private NewsReadService newsReadService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.news.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public News findById(Long id){
        Response<News> response = newsReadService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find news read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
    
    /**
     * 创建一条信息
     * @param news
     * @return
     */
    @OpenMethod(
            key = "create.news",
            paramNames = {"news"},
            httpMethods = RequestMethod.POST
    )
    public Long create(News news){
        Response<Long> response = newsWriteService.create(news);
        if(!response.isSuccess()){
            log.error("failed to create news read by news = {}, cause : {}", news, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据新闻类型type查询
     * @param type
     * @return
     */
    @OpenMethod(
            key = "query.ListNews.by.type",
            paramNames = {"type"},
            httpMethods = RequestMethod.GET
    )
    public List<News> findByType(Integer type){
        Response< List<News>> response = newsReadService.finByType(type);
        if (!response.isSuccess()){
            log.error("failed to find List<news> read by type = {}, cause : {}", type, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();

    }


    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @param type
     * @return
     *
     *
     * 这里的status是否也要在这里加上判断, 不然失效的信息也会显示出来.在type后面再加一个状态参数(status).?
     */
    @OpenMethod(
            key = "find.paging.news",
            paramNames = {"pageNo","pageSize","type"},
            httpMethods = RequestMethod.GET
    )
    public Paging<News> pagingBreedReport(Integer pageNo, Integer pageSize, Integer type){
        if(type == null){
            log.error("type is null");
            throw new OPClientException("type is null");
        }
        Response<Paging<News>> pagingResponse = newsReadService.pagingNews(pageNo,pageSize,type);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging news type {}, cause : {}", type, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

}
